<?php

namespace App\HttpController\Admin;

use App\HttpController\BaseController;
use App\Model\Admins\AdminsBean;
use App\Model\Admins\AdminsModel;
use App\Utility\Captcha;
use App\Utility\Token;


class Login extends BaseController {

    public function index(){
        //  getQueryParam
        $request = $this->request();
        $data = $request->getRequestParam();
        //验证验证码
        $check = (new Captcha())->check($data['captcha'],$data['captchaKey']);
        if($check === false){
            return $this->writeJson(1,'','验证码错误');
        }
        //判断用户非法
        $bean = new AdminsBean($data);
        $model = new AdminsModel();
        if($model->check($bean)){
            $token = Token::sessionEncrypt($data['username']);
            return $this->writeJson(0,$token,'登录成功，页面即将跳转...');
        }
        return $this->writeJson(1,'','用户名或者密码输入错误');
    }


    public function captcha(){
        $captcha = (new Captcha())->create();
        return $this->writeJson(200,$captcha,'SECCUSS');
    }
}

